package com.burgeon.backend.EnterpriseWeChat.rest.Brand;

import com.burgeon.backend.EnterpriseWeChat.entity.DIM;
import com.burgeon.backend.EnterpriseWeChat.entity.DimDef;
import com.burgeon.backend.EnterpriseWeChat.rest.base.RESTBase;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 购物车推荐
 */
public class getDIMData extends RESTBase {
    private final static String RANGE = "20";//分页数量
    private String ip;
    private String account;
    private String password;

    public getDIMData(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     *
     * @return
     */

    public DIM getData(String dimId) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(dimId), url);
        System.err.println(str);
        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)) {
            throw new Exception(new JSONArray(str).getJSONObject(0).getString("message"));
        }

        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        ArrayList<DimDef> dimDefs= new ArrayList<>();
        if(jsonArray.length()>0){
                DIM dimDef = new DIM();
                dimDef.setId(jsonArray.getJSONArray(0).getString(0));
                dimDef.setDimflag(jsonArray.getJSONArray(0).getString(1).replace("属性","DIM"));
                dimDef.setName(jsonArray.getJSONArray(0).getString(2));
//                System.err.println(dimDef);
            return dimDef;

        }
//        System.err.println(dimDefs.size());
        return null;
    }

    private MultiValueMap Data(String dimId) {
        HashMap params = new HashMap();
        ArrayList colums = new ArrayList();//查询字段

        colums.add("ID");
        colums.add("DIMFLAG");//属性标志
        colums.add("'NAME'");//属性名称

        params.put("columns", colums);
        params.put("table", "M_DIMDEF");//表名
        params.put("count", "true");

        HashMap paramss = new HashMap();

        paramss.put("column","'ID'");
        String a = "'="+dimId+"'";
        paramss.put("condition",a);

        HashMap orderby = new HashMap();

        orderby.put("column", "ID");
        orderby.put("asc", "false");
        ArrayList list = new ArrayList();
        list.add(orderby);

        params.put("orderby", list);

        params.put("params", paramss);
//        System.err.println(paramss);
        return DATA("Query", params, account, password);
    }

    public static void main(String[] args) {
        getDIMData g = new getDIMData("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");
        try {
            System.err.println(g.getData("2"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
